export default {
    namespaced: true,
    state: {
        currentType: '',
        currentEditBlockType: "",
        currentOptIndex: -1,
        topic: {}
    },
    actions: {
        setTopic(context, value) {
            // console.log(value)
            if (!value) return;//undefined和null都是false
            const reg = /<p>(?<content>.*)<\/p>/;
            const result = reg.exec(value);
            // console.log(result)
            if (result && result.groups) {
                context.commit("SET_TOPIC", result.groups.content)
            }
        },
    },
    mutations: {
        // value:{key,value}
        SET_TOPIC(state, value) {
            // const {currentEditBlockType } = state;
            const { currentEditBlockType, currentOptIndex } = state;
            if (currentEditBlockType == 'opt') {
                // 只有重新生成一个数组不然只有api
                const newOpts = state.topic.opts.map((v, i) => i == currentOptIndex ? value : v)
                state.topic.opts = newOpts;
            } else {
                state.topic[currentEditBlockType] = value;
            }
        },
        // 设置当前编辑的区域是什么类型
        SET_CURRENTEDITBLOCKTYPE(state, value) {
            state.currentEditBlockType = value
        },
        // 设置当前是哪种题型
        SET_CURRENTTYPE(state, value) {
            state.currentType = value;
        },
        // 初始化topic的
        INIT_TOPIC(state, value) {
            state.topic = value;
        },
        // 添加选项的
        ADD_OPT(state) {
            state.topic.opts.push('示例选项')
        },
        // 设置编辑的opt的索引
        SET_CURRENTOPTINDEX(state, value) {
            state.currentOptIndex = value;
        },
        // 设置正确选项的回调
        SET_CORRECT(state, value) {
            const { currentType } = state;
            if (currentType == 'mc') {
                // 应用mc的逻辑
                // const { corrects } = state.topic;
                // const order = this.getOrder(index);
                const pos = state.topic.corrects.indexOf(value);
                if (pos == -1) {
                    // 向choices中加入opt
                    state.topic.corrects.push(value);
                } else {
                    // 从choices中移除opt
                    state.topic.corrects.splice(pos, 1);
                }
            } else if (currentType == 'sc') {
                state.topic.correct = value
            } else if (currentType == 'tof') {
                state.topic.judgeRes = value;
            } else if (currentType == 'bl') {
                state.topic.answer.push(value)
            } else if (currentType == 'an') {
                state.topic.answer = value
            }
        }
    }
}